package com.bjpowernode.web;

import com.bjpowernode.entity.TActivity;
import com.bjpowernode.query.ActivityQuery;
import com.bjpowernode.query.ActivityRemarkQuery;
import com.bjpowernode.result.R;
import com.bjpowernode.service.ActivityRemarkService;
import com.bjpowernode.service.ActivityService;
import com.github.pagehelper.PageInfo;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 */
@RestController
@Slf4j
@RequestMapping("/api/activity")
public class ActivityController {

    @Resource
    private ActivityService activityService;

    @Resource
    private ActivityRemarkService activityRemarkService;

    @GetMapping("")
    public R activityByPage(@RequestParam(value = "current", defaultValue = "1") Integer current , ActivityQuery activityQuery) {
        log.info("current : {}  ,  activityQuery {}", current , activityQuery);
        PageInfo<TActivity> activityPageInfo = activityService.getActivityByPage(current , activityQuery);
        log.info("值 : " + activityPageInfo.getPageSize());
        return R.OK(activityPageInfo);
    }

    @PostMapping("")
    public R save(@Validated ActivityQuery activityQuery, @RequestHeader(value = "Authorization") String jwt) {
        log.info("添加 , 数据为 : {}", activityQuery);
        activityQuery.setJwt(jwt);
        int save = activityService.save(activityQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    @GetMapping("{id}")
    public R getActivityById(@PathVariable Integer id) {
        log.info("要获取的活动id : {}", id);
        TActivity tActivity = activityService.getActivityById(id);
        return R.OK(tActivity);
    }

    @PutMapping("")
    public R updateUser(@Validated ActivityQuery activityQuery,
                        @RequestHeader(value = "Authorization") String jwt) {
        log.info("要修改的活动信息 : {}", activityQuery);
        // 修改实现
        activityQuery.setJwt(jwt);
        int count = activityService.updataUserById(activityQuery);
        return count >= 1 ? R.OK() : R.FAIL();
    }

    @PostMapping("remark")
    public R addActivityRemark(@Validated @RequestBody ActivityRemarkQuery activityRemarkQuery , @RequestHeader(value = "Authorization") String jwt) {
        log.info("要保存的备注信息activityRemarkQuery : {} " , activityRemarkQuery);
        activityRemarkQuery.setJwt(jwt);
        int save = activityRemarkService.save(activityRemarkQuery);
        return save >= 1 ? R.OK() : R.FAIL();
    }

    @DeleteMapping("{id}")
    public R remove(@PathVariable Integer id) {
        log.info("要删除的活动id : {}", id);
        int delete = activityService.removeActivityById(id);
        return delete >= 1 ? R.OK() : R.FAIL();
    }

    @DeleteMapping("batchRemove")
    public R batchRemove(@RequestParam(value = "ids")String ids) {
        log.info("要批量删除的活动id : {}" , ids);
        // 将字符串 转换成数组
        String[] idsStringArray = ids.split(",");
        List<Integer> idList = Arrays.stream(idsStringArray)
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        int batched = activityService.batchDeleteActivitysByIds(idList);
        return batched > 0 ? R.OK() : R.FAIL();
    }
}
